package br.com.android.rtgym;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.SimpleExpandableListAdapter;
import android.widget.TextView;
import br.com.android.rtgym.beans.Aluno;
import br.com.android.rtgym.beans.Exercicio;
import br.com.android.rtgym.beans.Treino;
import br.com.android.rtgym.services.TreinosUsuario;

import com.android.rtgym.R;

public class Treinos extends Activity {

	private static final String NAME = "NAME";
	private static final String IS_EVEN = "IS_EVEN";

	private ExpandableListAdapter mAdapter;
	private ArrayList<Treino> treino;
	private TextView nomeAluno;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.treinos);
		List<Map<String, String>> groupData = new ArrayList<Map<String, String>>();
		List<List<Map<String, String>>> childData = new ArrayList<List<Map<String, String>>>();

		treino = (ArrayList<Treino>) getIntent().getSerializableExtra(
				"treinosAluno");
		
		nomeAluno = (TextView) findViewById(R.id.nomeAluno);
		nomeAluno.setText(treino.get(0).getAluno().getNome() + " "
				+ treino.get(0).getAluno().getSobreNome());

		for (Treino treinoList : treino) {
			Map<String, String> curGroupMap = new HashMap<String, String>();
			groupData.add(curGroupMap);
			curGroupMap.put(NAME, treinoList.getTitulo());

			List<Map<String, String>> children = new ArrayList<Map<String, String>>();
			for (Exercicio exercicio : treinoList.getExercicio()) {
				Map<String, String> curChildMap = new HashMap<String, String>();
				children.add(curChildMap);
				curChildMap.put(NAME, exercicio.getNomeExercicio());
				curChildMap.put(IS_EVEN, exercicio.getDetalheExercicio());
			}
			childData.add(children);
		}

		// Set up our adapter
		mAdapter = new SimpleExpandableListAdapter(this, groupData,
				android.R.layout.simple_expandable_list_item_1, new String[] {
						NAME, IS_EVEN }, new int[] { android.R.id.text1,
						android.R.id.text2 }, childData,
				android.R.layout.simple_expandable_list_item_2, new String[] {
						NAME, IS_EVEN }, new int[] { android.R.id.text1,
						android.R.id.text2 });

		ExpandableListView listView = (ExpandableListView) findViewById(R.id.listCadastro);
		listView.setAdapter(mAdapter);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.activity_entrada, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		Intent intentTreinoAluno = new Intent(this, AlunoNotificacao.class);
		intentTreinoAluno.putExtra("aluno", getIntent().getSerializableExtra("aluno"));
		startActivity(intentTreinoAluno);
		return true;
	}

}
